package com.zhijian.wharf.controller.web;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.annotation.LogOperation;
import com.zhijian.wharf.entity.po.Role;
import com.zhijian.wharf.entity.vo.request.RoleSearchVo;
import com.zhijian.wharf.entity.vo.response.RoleVo;
import com.zhijian.wharf.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色信息 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-22 15:37:30
 */
@Slf4j
@Controller
@Api(tags = "角色")
public class RoleController {

    @Autowired
    private RoleService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/role")
    @ApiOperation("新增")
    @LogOperation("新增角色")
    public boolean insert(@RequestBody Role param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/role/{id}")
    @ApiOperation("删除")
    @LogOperation("删除角色")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/role/{id}")
    @ApiOperation("修改")
    @LogOperation("修改部门")
    public boolean update(@PathVariable("id") Long id, @RequestBody Role param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/role/{id}")
    @ApiOperation("根据id查询")
    @LogOperation("查看部门详情")
    public RoleVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/role/page")
    @ApiOperation("分页查询")
    @LogOperation("分页查询部门")
    public Page<RoleVo> selectByCondition(@RequestBody(required = false) RoleSearchVo param) {
        if (param == null)
            param = RoleSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 列表查询，不分页
     *
     * @return
     */
    @Response
    @GetMapping("/role/list")
    @ApiOperation("列表查询，不分页")
    @LogOperation("列表查询部门")
    public List<RoleVo> selectList() {
        return service.selectList(RoleSearchVo.builder().build());
    }

    /**
     * 保存菜单与角色的关系
     *
     * @param id      角色id
     * @param menuIds 菜单id集合
     * @return
     */
    @Response
    @PostMapping("role/menu/{id}")
    @ApiOperation("保存菜单与角色的关系")
    public boolean saveMenus(@ApiParam(name = "id", value = "角色id") @PathVariable("id") Long id,
                             @ApiParam(name = "menuIds", value = "菜单id数组") @RequestBody List<Long> menuIds) {
        return service.connectMenus(id, menuIds);
    }

}